package generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author JohnSoybean
 * @create 2020/6/4 16:59
 * @description MybatisPlus代码生成器
 */
public class CodeGenerator {

    public static void main(String[] args) {
        //创建代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略
        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("JohnSoybean");
        gc.setOpen(false);  //是否打开目录
        gc.setFileOverride(false); //是否覆盖
        gc.setServiceName("%sService"); //去掉Service的I前缀
        gc.setDateType(DateType.ONLY_DATE); //搞清楚DateType的类型
        gc.setSwagger2(false);       //CFG 是否开启 Swagger
        gc.setIdType(IdType.AUTO);  //主键策略
        mpg.setGlobalConfig(gc);
        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        //CFG 数据库配置
        dsc.setUrl("jdbc:mysql://rm-bp167vga5admjnfdb3o.mysql.rds.aliyuncs.com:3306/baseball?characterEncoding=utf8&autoReconnect=true&useTimezone=true&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("baseball");
        dsc.setPassword("Baseball2020");
        mpg.setDataSource(dsc);
        //3、包的配置
        PackageConfig pc = new PackageConfig();
        //CFG 包名配置
//        pc.setModuleName("generate");
        pc.setParent("com.lxk");
        pc.setEntity("pojo");
        pc.setMapper("dao");
        pc.setXml("dao.xml");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //CFG 配置需映射的表名
        strategy.setInclude(
                "activity",
                "category",
                "consume_order",
                "device",
                "product",
                "recharge",
                "recharge_order",
                "rules",
                "ticket",
                "type",
                "user_ticket",
                "users",
                "wx_user"
                );
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true); // 自动lombok；
        //CFG 逻辑删除标志
        strategy.setLogicDeleteFieldName("deleted");
        //CFG 自动填充配置
        TableFill createTime = new TableFill("create_Time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_Time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //CFG 乐观锁
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true); //localhost:8080/hello_id_2
        mpg.setStrategy(strategy);
        mpg.execute(); //执行
    }


}
